<!DOCTYPE html>
<html>

<head>
	<title>小水滴</title>
	<meta name="keywords"  />
	<meta name="description" content="" />
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
	<!-- Custom Theme files 1280*559-->
	<link href="/static/css/font/iconfont.css" rel="stylesheet">
  	<link href="/static/js/layui/css/layui.css" rel="stylesheet">
	  <link href="/static/js/layui/css/form.css" rel="stylesheet">
	<!-- //Custom Theme files -->
	<!-- 青、赤、黄、白、黑 -->
  	<script src="/static/js/layui/layui.js"></script>
  	<script src="/static/js/common.js"></script>
	<script> var iframe = 'appAdd'; </script>
</head>

<body class="win-page">
	<div class="layui-form edit" lay-filter="edit" >
		<div class="layui-form-item">
			<label class="layui-form-label required">应用名称</label>
			<div class="layui-input-block">
				<input type="text" name="name" lay-verify="required" lay-vertype="tips" placeholder="请输入名称" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label required">应用简介</label>
			<div class="layui-input-block">
				<input type="text" name="remarks" lay-verify="" lay-vertype="tips" placeholder="请输入简介" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label required">归属标品</label>
			<div class="layui-input-block">
				<select name="sysAppId" lay-search="" lay-verify="required"></select>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label required">归属项目</label>
			<div class="layui-input-block">
				<select name="projectId" lay-search="" lay-verify=""></select>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label required">产品</label>
			<div class="layui-input-block">
				<input type="text" name="cp" lay-verify="" lay-vertype="tips" placeholder="请输入产品姓名" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label required">研发</label>
			<div class="layui-input-block">
				<input type="text" name="yf" lay-verify="" lay-vertype="tips" placeholder="请输入研发姓名" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label required">测试</label>
			<div class="layui-input-block">
				<input type="text" name="cs" lay-verify="" lay-vertype="tips" placeholder="请输入测试姓名" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label"></label>
			<div class="layui-input-block win-btns">
				<button class="layui-btn layui-btn-sm" type="submit" lay-submit lay-filter="save" id="save">保存</button>
				<button class="layui-btn layui-btn-sm" id="close">关闭</button>
			</div>
		</div>
	</div>
</body>
<script>

	$(function(){
		$('input[name="name"]').focus();

		
	});

	$('#close').click(function(){
		c.closePopWindow('appAdd');
	});

	$('#save').click(function(){
		form.on('submit(save)', function(data){
			// form 验证通过才会进来
			var field = data.field;
			var rps = c.getRequestParams();

			var url = rps.id ? 'app/update' : 'app/add';
			field.id = rps.id ? rps.id : '';

			c.request(url,field,function(resp){
				if( resp.code == 200 ){
					c.message('保存成功',parent.layer);
					parent.reloadTable();
					c.closePopWindow('appAdd');
				}else{
					c.message(resp.message);
				}
			});
			
			return false; // 阻止默认 form 跳转
		});
	});

	c.request('sysApp/page',{page:1,limit:1000},function(resp){
		if( resp.code == 0 ){
			var ele = $('select[name="sysAppId"]');
			var list = resp.data.list;
			ele.empty();
			ele.append('<option value="">请选择或搜索</option>');
			for( var i=0 ; i<list.length ; i++ ){
				var row = list[i];
				ele.append('<option value="'+row.id+'">'+row.name+'</option>');
			}
			form.render(ele);
			initPage();
		}else{
			c.message(resp.message);
		}
	});

	c.request('project/page',{page:1,limit:1000},function(resp){
		if( resp.code == 0 ){
			var ele = $('select[name="projectId"]');
			var list = resp.data.list;
			ele.empty();
			ele.append('<option value="-1">请选择或搜索</option>');
			for( var i=0 ; i<list.length ; i++ ){
				var row = list[i];
				ele.append('<option value="'+row.id+'">'+row.cname+'</option>');
			}
			form.render(ele);
			initPage();
		}else{
			c.message(resp.message);
		}
	});

	var maxRequestLen = 2,currRequestLen = 0;

	function initPage(){
		currRequestLen++;

		if( currRequestLen != maxRequestLen ){
			return;
		}

		var rps = c.getRequestParams();
		if( rps.id ){
			c.request('app/get',{id:rps.id},function(resp){
				if( resp.code == 200 ){
					form.val('edit',resp.data);
				}else{
					c.message(resp.message);
				}
			});
		}
	}

</script>
</html>